//
//  HNSH_HomeRecommendCell1.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/5/25.
//

#import "HNSH_HomeRecommendCell1.h"
#import "HNSH_GoodsDetailViewController.h"

@interface HNSH_HomeRecommendCellItem1 : UIView

/* imgV */
@property (nonatomic, strong) UIImageView *imgV;
/* titleLabel */
@property (nonatomic, strong) UILabel *titleLabel;
/* moneyLabel */
@property (nonatomic, strong) UILabel *moneyLabel;

/* clickBlock */
@property (nonatomic, copy) dispatch_block_t clickBlock;

@end

@implementation HNSH_HomeRecommendCellItem1

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self addAllViews];
        [self addAllLayout];
    }
    return self;
}

- (void)addAllViews {
    [self addSubview:self.imgV];
    [self addSubview:self.titleLabel];
    [self addSubview:self.moneyLabel];
    
    self.userInteractionEnabled = YES;
    __weak __typeof(&*self)weakSelf = self;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if (weakSelf.clickBlock) {
            weakSelf.clickBlock();
        }
    }];
    [self addGestureRecognizer:tap];
}

- (void)addAllLayout {
    [self.imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.width.height.mas_equalTo(74);
        make.centerX.equalTo(self.mas_centerX);
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(self.imgV.mas_bottom).offset(8);
        make.height.mas_equalTo(16);
    }];
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(self.titleLabel.mas_bottom).offset(2);
        make.height.mas_equalTo(16);
    }];
}

- (UIImageView *)imgV{
    if (!_imgV) {
        _imgV = [UIImageView new];
        _imgV.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _imgV;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor appBlackLabelColor];
        _titleLabel.font = [UIFont systemFontOfSize:11];
    }
    return _titleLabel;
}

- (UILabel *)moneyLabel {
    if (!_moneyLabel) {
        _moneyLabel = [UILabel new];
        _moneyLabel.textColor = [UIColor appGreenColor];
        _moneyLabel.font = [UIFont boldSystemFontOfSize:11];
    }
    return _moneyLabel;
}

@end


@interface HNSH_HomeRecommendCell1 ()

/* bgView */
@property (nonatomic, strong) UIImageView *bgView;
/* leftTitleImgV */
@property (nonatomic, strong) UIImageView *leftTitleImgV;
/* centerImgV */
@property (nonatomic, strong) UIImageView *centerImgV;
/* rightTitleImgV */
@property (nonatomic, strong) UIImageView *rightTitleImgV;


@end

@implementation HNSH_HomeRecommendCell1

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        [self configUI];
    }
    return self;
}

- (void)setModel:(HNSH_HomeViewModel *)model {
    _model = model;
    [self.bgView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    CGFloat spece = ((kScreenWidth-24)/2-12-74*2)/2;
    for (int i = 0; i < (model.yhh_list.count > 2 ? 2 : model.yhh_list.count); i ++) {
        HNSH_GoodsListModel *goodsModel = model.yhh_list[i];
        HNSH_HomeRecommendCellItem1 *goods = [[HNSH_HomeRecommendCellItem1 alloc]initWithFrame:CGRectMake(12+(74+spece)*i, 44, 74, 128)];
        [goods.imgV sd_setImageWithURL:[NSURL URLWithString:goodsModel.cover_url]];
        goods.titleLabel.text = goodsModel.name;
        goods.moneyLabel.text = [NSString stringWithFormat:@"¥%.2f",[goodsModel.price doubleValue]];
        [self.bgView addSubview:goods];
        goods.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
            HNSH_GoodsDetailViewController *vc = [[HNSH_GoodsDetailViewController alloc]init];
            vc.goodsId = goodsModel.uid;
            [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
        }];
        [goods addGestureRecognizer:tap];
    }
    
    for (int i = 0; i < (model.jrjx_list.count > 2 ? 2 : model.jrjx_list.count); i ++) {
        HNSH_GoodsListModel *goodsModel = model.jrjx_list[i];
        HNSH_HomeRecommendCellItem1 *goods = [[HNSH_HomeRecommendCellItem1 alloc]initWithFrame:CGRectMake((kScreenWidth-24)/2+spece+(74+spece)*i, 44, 74, 128)];
        [goods.imgV sd_setImageWithURL:[NSURL URLWithString:goodsModel.cover_url]];
        goods.titleLabel.text = goodsModel.name;
        goods.moneyLabel.text = [NSString stringWithFormat:@"¥%.2f",[goodsModel.price doubleValue]];
        [self.bgView addSubview:goods];
        goods.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
            HNSH_GoodsDetailViewController *vc = [[HNSH_GoodsDetailViewController alloc]init];
            vc.goodsId = goodsModel.uid;
            [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
        }];
        [goods addGestureRecognizer:tap];
    }
}

- (void)configUI {
    [self.contentView addSubview:self.bgView];
    [self.contentView addSubview:self.leftTitleImgV];
    [self.contentView addSubview:self.centerImgV];
    [self.contentView addSubview:self.rightTitleImgV];
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(12);
        make.right.mas_equalTo(-12);
    }];
    [self.leftTitleImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(12);
        make.left.mas_equalTo(24);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(20);
    }];
    [self.centerImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(12);
        make.centerX.equalTo(self.contentView.mas_centerX);
        make.width.mas_equalTo(1);
        make.height.mas_equalTo(106);
    }];
    [self.rightTitleImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(12);
        make.left.mas_equalTo(kScreenWidth/2+12);
        make.width.mas_equalTo(88);
        make.height.mas_equalTo(20);
    }];
    
    
    
}

#pragma mark lazy
- (UIImageView *)bgView {
    if (!_bgView) {
        _bgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_recommend_bg_1"]];
        _bgView.userInteractionEnabled = YES;
    }
    return _bgView;
}

- (UIImageView *)leftTitleImgV {
    if (!_leftTitleImgV) {
        _leftTitleImgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_recommend_title_1"]];
    }
    return _leftTitleImgV;
}

- (UIImageView *)centerImgV {
    if (!_centerImgV) {
        _centerImgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_recommend_line_1"]];
    }
    return _centerImgV;
}

- (UIImageView *)rightTitleImgV {
    if (!_rightTitleImgV) {
        _rightTitleImgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_recommend_title_2"]];
    }
    return _rightTitleImgV;
}

@end
